package com.itheima.health.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.RoleMapper;
import com.itheima.health.dao.RoleMenuMapper;
import com.itheima.health.dao.RolePermissionMapper;
import com.itheima.health.dao.UserRoleMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.RoleMenu;
import com.itheima.health.pojo.RolePermission;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@DubboService
public class RoleServiceImpl implements RoleService {
	@Autowired
	RoleMapper roleMapper;
	@Autowired
	RolePermissionMapper rolePermissionMapper;
	@Autowired
	UserRoleMapper userRoleMapper;
	@Autowired
	RoleMenuMapper roleMenuMapper;
	@Override
	public List<Role> findAllRoles() {
		List<Role> roleList = roleMapper.selectAll();
		return roleList;
	}

	@Override
	public List<Integer> findMenuIdsById(Integer id) {
		RoleMenu roleMenu = new RoleMenu();
		roleMenu.setRoleId(id);
		List<RoleMenu> roleMenuList = roleMenuMapper.select(roleMenu);
		List<Integer> list = new ArrayList<>();
		for (RoleMenu menu : roleMenuList) {
			list.add(menu.getMenuId());
		}
		return list;
	}

	@Override
	@Transactional
	public void delete(Integer id) {
		RolePermission rolePermission = new RolePermission();
		rolePermission.setRoleId(id);
		rolePermissionMapper.delete(rolePermission);
		roleMapper.deleteByPrimaryKey(id);
	}

	@Override
	public Role findById(Integer id) {
		log.info("[角色id===2]id:{}", id);
		Role role = roleMapper.selectByPrimaryKey(id);
		log.info("[查到的角色]role:{}", role);
		return role;
	}

	@Override
	@Transactional
	public void update(Integer[] permissionsIds, Integer[] menuIds, Role role) {
		log.info("[权限组---角色]permissionsIds:{},role:{}",permissionsIds,role);
		roleMapper.updateByPrimaryKeySelective(role);
		RoleMenu roleMenu = new RoleMenu();
		roleMenu.setRoleId(role.getId());
		roleMenuMapper.delete(roleMenu);
		//维护中间关联关系表t_role_menu
		roleMenuMapper.insertMany(role.getId(),menuIds);


		RolePermission rolePermission = new RolePermission();
		rolePermission.setRoleId(role.getId());
		rolePermissionMapper.delete(rolePermission);
		//维护中间关联关系表t_role_permission
		rolePermissionMapper.insertMany(role.getId(),permissionsIds);

	}

	@Override
	public List<Integer> findpermissionsIdsById(Integer id) {
		RolePermission rolePermission = new RolePermission();
		rolePermission.setRoleId(id);
		List<RolePermission> rolePermissions = rolePermissionMapper.select(rolePermission);
		List<Integer> list = new ArrayList<>();
		for (RolePermission permission : rolePermissions) {
			list.add(permission.getPermissionId());
		}
		return list;
	}

	@Override
	public void add(Integer[] menuIds, Integer[] permissionsIds, Role role) {
		roleMapper.insertSelective(role);
		//维护中间关联关系表t_role_menu
		roleMenuMapper.insertMany(role.getId(),menuIds);
		//维护中间关联关系表t_role_permission
		rolePermissionMapper.insertMany(role.getId(),permissionsIds);
	}

	@Override
	public PageResult findPage(QueryPageBean queryPageBean) {
		PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
		String queryString = queryPageBean.getQueryString();
		Example example = new Example(Role.class);
		Example.Criteria criteria = example.createCriteria();
		if (!StringUtils.isEmpty(queryString)) {

			criteria.orLike("name", "%" + queryString + "%");
			criteria.orLike("keyword", "%" + queryString + "%");


		}
		Page<Role> page = (Page<Role>) roleMapper.selectByExample(example);


		return new PageResult(page.getTotal(), page.getResult());
	}
}
